package com.cetcs.kmga.dataManager.service.impl.dataManager;

import com.cetc.cloud.kmga.util.DateUtils;
import com.cetcs.kmga.common.Data;
import com.cetcs.kmga.dataManager.dao.mybatis.AuditManagerMapper;
import com.cetcs.kmga.dataManager.dao.mybatis.FeatureCodeMapper;
import com.cetcs.kmga.dataManager.dao.mybatis.UsePatternAnalysisMapper;
import com.cetcs.kmga.dataManager.entity.dataManager.Resrc;
import com.cetcs.kmga.dataManager.entity.dataManager.vo.*;
import com.cetcs.kmga.dataManager.entity.device.vo.KeyAndValueVo;
import com.cetcs.kmga.dataManager.entity.global.IdAndValVo;
import com.cetcs.kmga.dataManager.entity.global.NameAndValue;
import com.cetcs.kmga.dataManager.service.dataManager.UsePatternAnalysisService;
import com.cetcs.kmga.dataManager.service.feature.FeatureCodeService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 类注释
 *
 * @author zhangchunlin
 * @version V1.0 创建时间：2017-10-10 15:31
 *          Copyright 2017 by CETC
 */
@Service
public class UsePatternAnalysisServiceImpl implements UsePatternAnalysisService {

    @Autowired
    private AuditManagerMapper auditManagerMapper;

    @Autowired
    private UsePatternAnalysisMapper usePatternAnalysisMapper;

    @Autowired
    private FeatureCodeMapper codeMapper;

    @Override
    public List<IdAndValVo> listAllResources(String keyword) throws Exception {
        Data dataMap = new Data( );
        if (!StringUtils.isEmpty(keyword)) {
            dataMap.put("name", keyword);
        }
        List<IdAndValVo> voList = Lists.newArrayList( );
        List<Resrc> resrcs = auditManagerMapper.getAllResrcByCondition(dataMap);
        if (resrcs != null && resrcs.size( ) > 0) {
            //过滤掉应用系统
            voList = resrcs.stream( ).filter(p -> !"016".equals(p.getResrcTypeCode( ))).map(p -> {
                IdAndValVo idAndValVo = new IdAndValVo( );
                idAndValVo.setValue(p.getName( ));
                idAndValVo.setId(String.valueOf(p.getId( )));
                return idAndValVo;
            }).collect(Collectors.toList( ));
        }
        return voList;
    }

    @Override
    public ResourceAnalysisVo getResourceById(String id, String keyTime) {
        String startDate = DateUtils.format(DateUtils.getTodayBefore(8), DateUtils.DATE_FORMAT_1);
        String endDate = DateUtils.format(DateUtils.getTodayBefore(1), DateUtils.DATE_FORMAT_1);
        if (org.apache.commons.lang.StringUtils.isNotBlank(keyTime)) {
            String dateRange = keyTime;
            String[] dateArr = dateRange.split("-");
            startDate = dateArr[0].trim( ) + "-" + dateArr[1].trim( ) + "-" + dateArr[2].trim( );
            endDate = dateArr[3].trim( ) + "-" + dateArr[4].trim( ) + "-" + dateArr[5].trim( );
            if(startDate.equals(endDate)){
                startDate=startDate+" 00";
                if(endDate.equals(DateUtils.format(new Date(),DateUtils.DATE_FORMAT_1))){
                    endDate=DateUtils.format(new Date(),DateUtils.DATE_FORMAT_5);
                }else {
                    endDate=endDate+" 23";
                }
            }
        }
        Data dataMap = new Data( );
        dataMap.put("id", id);
        ResourceAnalysisVo ret = new ResourceAnalysisVo( );
        try {
            Resrc resrc = auditManagerMapper.findResrcWithReplceCodeById(id);
            if (resrc == null) {
                return ret;
            }
            ret.setId(resrc.getId( ));
            ret.setName(resrc.getName( ));
            ret.setIp(resrc.getServerIp( ));
            ret.setWeight(resrc.getSupevLevel( ));
            ret.setTypeName(resrc.getResrcTypeCode( ));
        } catch (Exception e) {
            e.printStackTrace( );
        }
        //流量,格式有要求,参考返回对象中的字段说明
        RiskEventVo eventVo = usePatternAnalysisMapper.statResourceRiskLevelNumByRescId(id, startDate, endDate);
        List<Integer> rankList = Lists.newArrayList( );
        if (eventVo == null) {
            rankList.add(0);
            rankList.add(0);
            rankList.add(0);
        } else {
            rankList.add(StringUtils.isEmpty(eventVo.getHighEventCount( )) ? 0 : Integer.parseInt(eventVo.getHighEventCount( )));
            rankList.add(StringUtils.isEmpty(eventVo.getMiddleEventCount( )) ? 0 : Integer.parseInt(eventVo.getMiddleEventCount( )));
            rankList.add(StringUtils.isEmpty(eventVo.getLowEventCount( )) ? 0 : Integer.parseInt(eventVo.getLowEventCount( )));
        }
        ret.setRank(rankList);
        Integer accessNum = usePatternAnalysisMapper.statResourceAccessNumByRescId(id, startDate, endDate);
        if (accessNum == null) {
            accessNum = 0;
        }
        ret.setFlow(accessNum);
        return ret;
    }

    @Override
    public List<Map<String, String>> getTableTraffic(String resourceId, String keyTime) {
        String startDate = DateUtils.format(DateUtils.getTodayBefore(8), DateUtils.DATE_FORMAT_1);
        String endDate = DateUtils.format(DateUtils.getTodayBefore(1), DateUtils.DATE_FORMAT_1);
        if (org.apache.commons.lang.StringUtils.isNotBlank(keyTime)) {
            String dateRange = keyTime;
            String[] dateArr = dateRange.split("-");
            startDate = dateArr[0].trim( ) + "-" + dateArr[1].trim( ) + "-" + dateArr[2].trim( );
            endDate = dateArr[3].trim( ) + "-" + dateArr[4].trim( ) + "-" + dateArr[5].trim( );
            if(startDate.equals(endDate)){
                startDate=startDate+" 00";
                if(endDate.equals(DateUtils.format(new Date(),DateUtils.DATE_FORMAT_1))){
                    endDate=DateUtils.format(new Date(),DateUtils.DATE_FORMAT_5);
                }else {
                    endDate=endDate+" 23";
                }
            }
        }
        List<IdAndValVo> dbList = usePatternAnalysisMapper.statTblAccessNumByRescId(resourceId, startDate, endDate);
        List<Map<String, String>> ret = Lists.newArrayList( );
        int i = 0;
        for (IdAndValVo valVo : dbList) {
            Map item = new HashMap( );
            item.put("id", ++i);
            item.put("name", valVo.getId( ));
            item.put("num", valVo.getValue( ));
            ret.add(item);
        }
        return ret;
    }

    @Override
    public List<HistogramChartVo> findIpAccessStatistic(String resourceId, String keyTime) {
        List<HistogramChartVo> ret = new LinkedList<>( );
        String startDate = DateUtils.format(DateUtils.getTodayBefore(8), DateUtils.DATE_FORMAT_1);
        String endDate = DateUtils.format(DateUtils.getTodayBefore(1), DateUtils.DATE_FORMAT_1);
        if (org.apache.commons.lang.StringUtils.isNotBlank(keyTime)) {
            String dateRange = keyTime;
            String[] dateArr = dateRange.split("-");
            startDate = dateArr[0].trim( ) + "-" + dateArr[1].trim( ) + "-" + dateArr[2].trim( );
            endDate = dateArr[3].trim( ) + "-" + dateArr[4].trim( ) + "-" + dateArr[5].trim( );
            if(startDate.equals(endDate)){
                startDate=startDate+" 00";
                if(endDate.equals(DateUtils.format(new Date(),DateUtils.DATE_FORMAT_1))){
                    endDate=DateUtils.format(new Date(),DateUtils.DATE_FORMAT_5);
                }else {
                    endDate=endDate+" 23";
                }
            }
        }
        //IP访问量排名
        {
            List<IdAndValVo> dbIpList = usePatternAnalysisMapper.statIpAccessNumByRescId(resourceId, startDate, endDate);
            HistogramChartVo ip = new HistogramChartVo( );
            ip.setName("IP访问量排名");
            ip.setXaxis(dbIpList.stream( ).map(p -> p.getValue( )).collect(Collectors.toList( )));
            ip.setYdata(dbIpList.stream( ).map(p -> p.getId( )).collect(Collectors.toList( )));
            ret.add(ip);
        }
        //IP来源分布
        {
            List<IdAndValVo> dbSourceList = usePatternAnalysisMapper.statIpSourceNumByRescId(resourceId, startDate, endDate);
            if (dbSourceList != null && dbSourceList.size( ) > 10) {
                List<IdAndValVo> startList = dbSourceList.subList(0, 9);
                List<IdAndValVo> endList = dbSourceList.subList(10, dbSourceList.size( ));
                long endNum = endList.stream( ).mapToLong(p -> Long.valueOf(p.getValue( ))).sum( );
                IdAndValVo otherVo = new IdAndValVo( );
                otherVo.setId("其他");
                otherVo.setValue(String.valueOf(endNum));
                dbSourceList = Lists.newArrayList( );
                dbSourceList.addAll(startList);
                dbSourceList.add(otherVo);
            }
            HistogramChartVo databaseAccess = new HistogramChartVo( );
            databaseAccess.setName("IP来源分布");
            List<NameAndValue> ydataList = dbSourceList.stream( ).map(p -> {
                NameAndValue nameAndValue = new NameAndValue( );
                nameAndValue.setValue(p.getValue( ));
                nameAndValue.setName(p.getId( ));
                return nameAndValue;
            }).collect(Collectors.toList( ));
            databaseAccess.setYdata(ydataList);
            ret.add(databaseAccess);
        }
        //操作类型分布
        {
            List<IdAndValVo> dbSourceList = usePatternAnalysisMapper.statOperTypeSourceNumByRescId(resourceId, startDate, endDate);
            HistogramChartVo operator = new HistogramChartVo( );
            operator.setName("操作类型分布");
            if (dbSourceList != null && dbSourceList.size( ) > 10) {
                List<IdAndValVo> startList = dbSourceList.subList(0, 9);
                List<IdAndValVo> endList = dbSourceList.subList(10, dbSourceList.size( ));
                long endNum = endList.stream( ).mapToLong(p -> Long.valueOf(p.getValue( ))).sum( );
                IdAndValVo otherVo = new IdAndValVo( );
                otherVo.setId("其他");
                otherVo.setValue(String.valueOf(endNum));
                dbSourceList = Lists.newArrayList( );
                dbSourceList.addAll(startList);
                dbSourceList.add(otherVo);
            }
            List<NameAndValue> ydataList = dbSourceList.stream( ).map(p -> {
                NameAndValue nameAndValue = new NameAndValue( );
                nameAndValue.setValue(p.getValue( ));
                nameAndValue.setName(p.getId( ));
                return nameAndValue;
            }).collect(Collectors.toList( ));
            operator.setYdata(ydataList);
            ret.add(operator);
        }
        return ret;
    }

    @Override
    public MultiLineChartVo findTrafficStatisticOfDay(String resourceId, String keyTime) {
        String startDate = DateUtils.format(DateUtils.getTodayBefore(8), DateUtils.DATE_FORMAT_1);
        String endDate = DateUtils.format(DateUtils.getTodayBefore(1), DateUtils.DATE_FORMAT_1);
        if (org.apache.commons.lang.StringUtils.isNotBlank(keyTime)) {
            String dateRange = keyTime;
            String[] dateArr = dateRange.split("-");
            startDate = dateArr[0].trim( ) + "-" + dateArr[1].trim( ) + "-" + dateArr[2].trim( );
            endDate = dateArr[3].trim( ) + "-" + dateArr[4].trim( ) + "-" + dateArr[5].trim( );
            if(startDate.equals(endDate)){
                startDate=startDate+" 00";
                if(endDate.equals(DateUtils.format(new Date(),DateUtils.DATE_FORMAT_1))){
                    endDate=DateUtils.format(new Date(),DateUtils.DATE_FORMAT_5);
                }else {
                    endDate=endDate+" 23";
                }
            }
        }
        int dayNum = DateUtils.daysBetween(DateUtils.parse(startDate, DateUtils.DATE_FORMAT_1), DateUtils.parse(endDate, DateUtils.DATE_FORMAT_1)) + 1;
        MultiLineChartVo.YdataFormat ydataFormat = new MultiLineChartVo.YdataFormat( );
        ydataFormat.setName("每日访问量规律");
        List<IdAndValVo> dbSourceList = usePatternAnalysisMapper.statAvgHourAccessNumByRescId(resourceId, startDate, endDate);
        List<String> ydataList = mockHourStr(false);
        DecimalFormat df = new DecimalFormat("#0.00");
        ydataList = ydataList.stream( ).map(p -> {
            List<String> currentList = dbSourceList.stream( ).filter(k -> p.equals(k.getId( ))).map(k -> k.getValue( )).collect(Collectors.toList( ));
            String currentStr = "0";
            if (currentList != null && currentList.size( ) > 0) {
                long num = Long.parseLong(currentList.get(0));
                if (num >= dayNum) {
                    long val = num / dayNum;
                    currentStr = String.valueOf(val);
                } else if (num > 0 && num < dayNum) {
                    double val = num*1.00 / dayNum;
                    currentStr = df.format(val);
                } else {
                    currentStr = String.valueOf(0);
                }
            }
            return currentStr;
        }).collect(Collectors.toList( ));
        ydataFormat.setData(ydataList);
        MultiLineChartVo ret = new MultiLineChartVo( );
        ret.setxAxis(mockHourStr(true));
        ret.setName("每日访问量规律");
        List<MultiLineChartVo.YdataFormat> dataList = Lists.newArrayList( );
        dataList.add(ydataFormat);
        ret.setDatas(dataList);
        return ret;
    }

    /**
     * 组合Y 组
     *
     * @return
     */
    private List<String> mockHourStr(boolean isHaveDian) {
        List<String> ret = Lists.newArrayList( );
        for (int i = 0; i < 24; i++) {
            if (isHaveDian) {
                ret.add(i + "点");
            } else {
                if (i < 10) {
                    ret.add("0" + i);
                } else {
                    ret.add("" + i);
                }
            }
        }
        return ret;
    }

    /**
     * 分析对象列表
     *
     * @param dataMap
     * @return
     */
    @Override
    public List<ResourceTreeDataObjectVo> getAnalysisObjectLists(Data dataMap) {
        List<KeyAndValueVo> sysClientClusters = codeMapper.findUpSysClientClusters( );
        List<ResourceTreeDataObjectVo> resourceTreeDataObjectVos = sysClientClusters.stream( ).map(p -> {
            ResourceTreeDataObjectVo objectVo = new ResourceTreeDataObjectVo( );
            objectVo.setName(p.getValue( ));
            objectVo.setId(p.getKey( ));
            return objectVo;
        }).collect(Collectors.toList( ));
        return resourceTreeDataObjectVos;
    }

    /**
     * 访问流量与访问次数
     *
     * @param dataMap
     * @return
     */
    @Override
    public AccessTrafficAndVisitsVo getAccessTrafficAndVisits(Data dataMap) {
        //更具资源id和时间范围获取 访问流量与访问次数
        AccessTrafficAndVisitsVo accessTrafficAndVisitsVo = new AccessTrafficAndVisitsVo( );
        accessTrafficAndVisitsVo.setCallNum("1654654人次");
        accessTrafficAndVisitsVo.setDataNum("15645MB");
        return accessTrafficAndVisitsVo;
    }

    /**
     * 访问资源分析 排名char图
     *
     * @return
     */
    @Override
    public List<HistogramChartVo> getAccessResourceAnalysisForChar(String cid, String ip, String keyTime) {
        String startDate = DateUtils.format(DateUtils.getTodayBefore(8), DateUtils.DATE_FORMAT_1);
        String endDate = DateUtils.format(DateUtils.getTodayBefore(1), DateUtils.DATE_FORMAT_1);
        if (org.apache.commons.lang.StringUtils.isNotBlank(keyTime)) {
            String dateRange = keyTime;
            String[] dateArr = dateRange.split("-");
            startDate = dateArr[0].trim( ) + "-" + dateArr[1].trim( ) + "-" + dateArr[2].trim( );
            endDate = dateArr[3].trim( ) + "-" + dateArr[4].trim( ) + "-" + dateArr[5].trim( );
            if(startDate.equals(endDate)){
                startDate=startDate+" 00";
                if(endDate.equals(DateUtils.format(new Date(),DateUtils.DATE_FORMAT_1))){
                    endDate=DateUtils.format(new Date(),DateUtils.DATE_FORMAT_5);
                }else {
                    endDate=endDate+" 23";
                }
            }
        }
        //资源访问次数排名
        List<IdAndValVo> dbSourceList = usePatternAnalysisMapper.statResrcAccessNumByClientIdOrIp(cid, ip, startDate, endDate);
        List<HistogramChartVo> histogramChartVoshistogramChartVos = Lists.newArrayList( );
        HistogramChartVo histogramChartVo1 = new HistogramChartVo( );
        histogramChartVo1.setName("资源访问次数排名");
        histogramChartVo1.setXaxis(dbSourceList.stream( ).map(p -> p.getValue( )).collect(Collectors.toList( )));
        histogramChartVo1.setYdata(dbSourceList.stream( ).map(p -> p.getId( )).collect(Collectors.toList( )));
        histogramChartVoshistogramChartVos.add(histogramChartVo1);

        //表访问量分布
        HistogramChartVo histogramChartVo2 = new HistogramChartVo( );
        histogramChartVo2.setName("表访问量分布");
        dbSourceList = usePatternAnalysisMapper.statTblAccessNumByClientIdOrIp(cid, ip, startDate, endDate);
        if (dbSourceList != null && dbSourceList.size( ) > 10) {
            List<IdAndValVo> startList = dbSourceList.subList(0, 9);
            List<IdAndValVo> endList = dbSourceList.subList(10, dbSourceList.size( ));
            long endNum = endList.stream( ).mapToLong(p -> Long.valueOf(p.getValue( ))).sum( );
            IdAndValVo otherVo = new IdAndValVo( );
            otherVo.setId("其他");
            otherVo.setValue(String.valueOf(endNum));
            dbSourceList = Lists.newArrayList( );
            dbSourceList.addAll(startList);
            dbSourceList.add(otherVo);
        }
        List<NameAndValue> ydataList = dbSourceList.stream( ).map(p -> {
            NameAndValue nameAndValue = new NameAndValue( );
            nameAndValue.setValue(p.getValue( ));
            nameAndValue.setName(p.getId( ));
            return nameAndValue;
        }).collect(Collectors.toList( ));
        histogramChartVo2.setYdata(ydataList);
        histogramChartVoshistogramChartVos.add(histogramChartVo2);

        //操作类型分布
        dbSourceList = usePatternAnalysisMapper.statOperTypeAccessNumByClientIdOrIp(cid, ip, startDate, endDate);
        HistogramChartVo histogramChartVo3 = new HistogramChartVo( );
        histogramChartVo3.setName("操作类型分布");
        ydataList = dbSourceList.stream( ).map(p -> {
            NameAndValue nameAndValue = new NameAndValue( );
            nameAndValue.setValue(p.getValue( ));
            nameAndValue.setName(p.getId( ));
            return nameAndValue;
        }).collect(Collectors.toList( ));
        histogramChartVo3.setYdata(ydataList);
        histogramChartVoshistogramChartVos.add(histogramChartVo3);

        //常用工具分布
        HistogramChartVo histogramChartVo4 = new HistogramChartVo( );
        dbSourceList = usePatternAnalysisMapper.statToolAccessNumByClientIdOrIp(cid, ip, startDate, endDate);
        histogramChartVo4.setName("常用工具分布");
        if (dbSourceList != null && dbSourceList.size( ) > 10) {
            List<IdAndValVo> startList = dbSourceList.subList(0, 9);
            List<IdAndValVo> endList = dbSourceList.subList(10, dbSourceList.size( ));
            long endNum = endList.stream( ).mapToLong(p -> Long.valueOf(p.getValue( ))).sum( );
            IdAndValVo otherVo = new IdAndValVo( );
            otherVo.setId("其他");
            otherVo.setValue(String.valueOf(endNum));
            dbSourceList = Lists.newArrayList( );
            dbSourceList.addAll(startList);
            dbSourceList.add(otherVo);
        }
        ydataList = dbSourceList.stream( ).map(p -> {
            NameAndValue nameAndValue = new NameAndValue( );
            nameAndValue.setValue(p.getValue( ));
            nameAndValue.setName(p.getId( ));
            return nameAndValue;
        }).collect(Collectors.toList( ));
        histogramChartVo4.setYdata(ydataList);
        histogramChartVoshistogramChartVos.add(histogramChartVo4);
        return histogramChartVoshistogramChartVos;
    }

    /**
     * 访问资源分析 每日流量规律
     *
     * @param dataMap
     * @return
     */
    @Override
    public MultiLineChartVo getAccessResourceAnalysisForDailyTrafficRules(Data dataMap) {
        MultiLineChartVo multiLineChartVo = new MultiLineChartVo( );
        multiLineChartVo.setName("每日流量规律");
        multiLineChartVo.setxAxis(Arrays.asList("2017-09-01", "2017-09-02", "2017-09-03"));
        List<String> titles = Arrays.asList("读取", "写入");
        multiLineChartVo.setDatas(TrafficStatisticServiceImpl.mockDatas(titles, multiLineChartVo));
        return multiLineChartVo;
    }

    /**
     * 活跃情况分析 排名char图
     *
     * @param dataMap
     * @return
     */
    @Override
    public List<HistogramChartVo> getActiveAnalysisForChar(Data dataMap) {
        List<HistogramChartVo> histogramChartVoshistogramChartVos = Lists.newArrayList( );
        HistogramChartVo histogramChartVo1 = new HistogramChartVo( );
        histogramChartVo1.setName("资源排名");
        histogramChartVo1.setXaxis(Arrays.asList("892", "1137", "2850", "3694", "4522"));
        histogramChartVo1.setYdata(Arrays.asList("172.23.221.119", "172.23.221.210", "222.161.234.123", "192.168.1.55", "178.231.26.204"));
        histogramChartVoshistogramChartVos.add(histogramChartVo1);

        HistogramChartVo histogramChartVo2 = new HistogramChartVo( );
        histogramChartVo2.setName("常用IP排名");
        histogramChartVo2.setXaxis(Arrays.asList("892", "1137", "2850", "3694", "4522"));
        histogramChartVo2.setYdata(Arrays.asList("172.23.221.119", "172.23.221.210", "222.161.234.123", "192.168.1.55", "178.231.26.204"));
        histogramChartVoshistogramChartVos.add(histogramChartVo2);

        HistogramChartVo histogramChartVo3 = new HistogramChartVo( );
        histogramChartVo3.setName("常用数据库账户排名");
        histogramChartVo3.setXaxis(Arrays.asList("892", "1137", "2850", "3694", "4522"));
        histogramChartVo3.setYdata(Arrays.asList("172.23.221.119", "172.23.221.210", "222.161.234.123", "192.168.1.55", "178.231.26.204"));
        histogramChartVoshistogramChartVos.add(histogramChartVo3);

        HistogramChartVo histogramChartVo4 = new HistogramChartVo( );
        histogramChartVo4.setName("常用工具排名");
        histogramChartVo4.setXaxis(Arrays.asList("892", "1137", "2850", "3694", "4522"));
        histogramChartVo4.setYdata(Arrays.asList("172.23.221.119", "172.23.221.210", "222.161.234.123", "192.168.1.55", "178.231.26.204"));
        histogramChartVoshistogramChartVos.add(histogramChartVo4);

        return histogramChartVoshistogramChartVos;
    }

    /**
     * 活跃情况分析 每日流量规律
     *
     * @return
     */
    @Override
    public MultiLineChartVo getActiveAnalysisForDailyTrafficRules(String cid, String ip, String keyTime) {
        String startDate = DateUtils.format(DateUtils.getTodayBefore(8), DateUtils.DATE_FORMAT_1);
        String endDate = DateUtils.format(DateUtils.getTodayBefore(1), DateUtils.DATE_FORMAT_1);
        if (org.apache.commons.lang.StringUtils.isNotBlank(keyTime)) {
            String dateRange = keyTime;
            String[] dateArr = dateRange.split("-");
            startDate = dateArr[0].trim( ) + "-" + dateArr[1].trim( ) + "-" + dateArr[2].trim( );
            endDate = dateArr[3].trim( ) + "-" + dateArr[4].trim( ) + "-" + dateArr[5].trim( );
            if(startDate.equals(endDate)){
                startDate=startDate+" 00";
                if(endDate.equals(DateUtils.format(new Date(),DateUtils.DATE_FORMAT_1))){
                    endDate=DateUtils.format(new Date(),DateUtils.DATE_FORMAT_5);
                }else {
                    endDate=endDate+" 23";
                }
            }
        }
        int dayNum = DateUtils.daysBetween(DateUtils.parse(startDate, DateUtils.DATE_FORMAT_1), DateUtils.parse(endDate, DateUtils.DATE_FORMAT_1)) + 1;
        MultiLineChartVo.YdataFormat ydataFormat = new MultiLineChartVo.YdataFormat( );
        ydataFormat.setName("每日访问量规律");
        List<IdAndValVo> dbSourceList = usePatternAnalysisMapper.statSumHourAccessNumByClientIdOrIp(cid, ip, startDate, endDate);
        List<String> ydataList = mockHourStr(false);
        DecimalFormat df = new DecimalFormat("#0.00");
        ydataList = ydataList.stream( ).map(p -> {
            List<String> currentList = dbSourceList.stream( ).filter(k -> p.equals(k.getId( ))).map(k -> k.getValue( )).collect(Collectors.toList( ));
            String currentStr = "0";
            if (currentList != null && currentList.size( ) > 0) {
                long num = Long.parseLong(currentList.get(0));
                if (num >= dayNum) {
                    long val = num / dayNum;
                    currentStr = String.valueOf(val);
                } else if (num > 0 && num < dayNum) {
                    double val = num*1.00 / dayNum;
                    currentStr = df.format(val);
                } else {
                    currentStr = String.valueOf(0);
                }
            }
            return currentStr;
        }).collect(Collectors.toList( ));
        ydataFormat.setData(ydataList);
        MultiLineChartVo ret = new MultiLineChartVo( );
        ret.setxAxis(mockHourStr(true));
        ret.setName("每日访问量规律");
        List<MultiLineChartVo.YdataFormat> dataList = Lists.newArrayList( );
        dataList.add(ydataFormat);
        ret.setDatas(dataList);
        return ret;
    }

    /**
     * 资源访问量排名
     *
     * @param dataMap startTime endTime
     * @return
     */
    @Override
    public List<RankOfBehaviorVo> getRankOfResourceAccess(Data dataMap) {
        //先找出所有的资源访问总量
        Integer total = usePatternAnalysisMapper.getAccessNumOfResourceAccess(dataMap);
        dataMap.put("total_access_num", total == null ? 1 : total);
        return usePatternAnalysisMapper.getRankOfResourceAccess(dataMap);
    }

    /**
     * 每日访问量规律
     *
     * @param dataMap
     * @return
     */
    @Override
    public List<NameAndValue> getDailyTrafficPatterns(Data dataMap) {
        return usePatternAnalysisMapper.getDailyTrafficPatterns(dataMap);
    }

    /**
     * 活跃IP统计（访问次数）
     *
     * @param dataMap
     * @return
     */
    @Override
    public List<RankOfBehaviorVo> getActiveIPStatistics(Data dataMap) {
        //先找出所有的活跃IP总量
        Integer total = usePatternAnalysisMapper.getAllActiveIPStatistics(dataMap);
        dataMap.put("total_access_num", total == null ? 1 : total);
        return usePatternAnalysisMapper.getActiveIPStatistics(dataMap);
    }

    /**
     * IP来源分布
     *
     * @param dataMap
     * @return
     */
    @Override
    public List<NameAndValue> getDistributionOfIPSource(Data dataMap) {
        return usePatternAnalysisMapper.getDistributionOfIPSource(dataMap);
    }

    /**
     * 操作类型分布
     *
     * @param dataMap
     * @return
     */
    @Override
    public List<NameAndValue> getDistributionOfOptType(Data dataMap) {
        return usePatternAnalysisMapper.getDistributionOfOptType(dataMap);
    }
}
